(function($, doc) {
	$.init();
	$.plusReady(function() {
		//var id = plus.webview.currentWebview().id;
		//console.log(id);
		//var settings = app.getSettings();
		
		var regButton = doc.getElementById('reg');
		var accountBox = doc.getElementById('account');
		var passwordBox = doc.getElementById('password');
		var tradepwdBox = doc.getElementById('tradepwd');
		var nicknameBox = doc.getElementById('nickname');
		var usernameBox = doc.getElementById('user_name');
		var sexBox = doc.getElementById('sex');
		var passwordConfirmBox = doc.getElementById('password_confirm');


		//localStorage.removeItem('_send_code');

		//点击获取验证码
		document.getElementById("getCode").addEventListener('tap', function() {
			//localStorage.removeItem('_send_code');
			var c1 = 60,
				phone = document.getElementById('account').value;
			//			if(!phone || !(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
			//				plus.nativeUI.toast("手机号码有误，请重填手机号");
			//				return;
			//			}
			if(!phone) {
				plus.nativeUI.toast("手机号码不能为空");
				return;
			}
			var user_name = document.getElementById('user_name').value;
			if(user_name.length < 3) {
				plus.nativeUI.toast("请输入要找回的账号");
				return;
			}
			
			//return;
			var btnArray = ['确定', '取消'];
			mui.confirm('我们将发送验证码短信到账号关联的手机号', '确认', btnArray, function(e) {
				if(e.index == 0) {

					mui.get(apiurl + 'index/getsms', {
						'user_name': user_name,
						'phone': phone
					}, function(res) {
						if(res.status == 1) {
							//发送短信成功
							//localStorage.setItem('_send_code', res.ck_code);
							plus.nativeUI.toast("发送成功！");
						} else {
							plus.nativeUI.toast(res.msg);
							document.getElementById('getCode').innerHTML = '获取验证码';
							document.getElementById('getCode').removeAttribute('disabled');
							c1 = 60;
							return;
						}
						var code1 = setInterval(function() {
							document.getElementById('getCode').innerHTML = c1 + '秒后重新获取';
							c1 = c1 - 1;
							if(c1 >= 0) {
								document.getElementById('getCode').setAttribute("disabled", "disabled");
							} else {
								document.getElementById('getCode').innerHTML = '获取验证码';
								document.getElementById('getCode').removeAttribute('disabled');
								c1 = 60;
								clearInterval(code1);
							}
						}, 1000);
					});

				}
			})

		});
		
		regButton.addEventListener('tap', function(event) {
			var regInfo = {
				wjtype: usubmit(),
				account: accountBox.value,
				username: usernameBox.value,
				password: md5(passwordBox.value),
				phone_country: doc.getElementById('country').value,
				phone_code: doc.getElementById('area_code').value,
				code: doc.getElementById('code').value
			};

			
			if(regInfo.username.length < 4) {
				plus.nativeUI.toast('用户名最短需要 4 个字符');
				return;
			}
			if(doc.getElementById('code').value == '') {
				plus.nativeUI.toast('请填写验证码');
				return;
			}

	
			console.log(JSON.stringify(regInfo));
			console.log(zhpwdurl);
			mui.post(zhpwdurl, regInfo, function(data) {
				console.log(JSON.stringify(data));
				//localStorage.removeItem('_send_code');
				if(!data.status) {
					if(data.msg) {
						mui.alert(data.msg);
					} else {
						mui.alert("注册失败！");
					}

					return;
				}
				
				mui.toast('修改密码成功');
				setTimeout(function() {
					location.href = 'login.html';
				}, 500);

				//toMain();
				/*
				plus.webview.getLaunchWebview().show("pop-in", 200, function() {
					plus.webview.currentWebview().close("none");
				});*/
			});

		});
	});

	//添加自定义事件监听
	window.addEventListener('update_code', function(event) {
		//获得事件参数
		var country = event.detail.name;
		var area_code = event.detail.code;
		//console.log(area_code);
		document.getElementById('type').value = country + '  (+' + area_code + ')';
		document.getElementById('country').value = country;
		document.getElementById('area_code').value = area_code;
	});
	function usubmit(){
		var radionum = document.getElementById("form1").radio1;
		for(var i=0;i<radionum.length;i++){
			if(radionum[i].checked){
			userid = radionum[i].value
			}
		}
		return userid;
		
	}

	function edit_nickname() {
		var ck_ed = cheche_edit();
		if(ck_ed) {
			document.getElementById('reg').removeAttribute('disabled');
		} else {
			document.getElementById('reg').setAttribute('disabled', 'disabled');
		}
	}

	function edit_phone(obj) {
		var ck_ed = cheche_edit();
		if(ck_ed) {
			document.getElementById('reg').removeAttribute('disabled');
		} else {
			document.getElementById('reg').setAttribute('disabled', 'disabled');
		}
		//
		//		if(!(/^1(3|4|5|7|8)\d{9}$/.test(obj.value))) {
		//			mui('.mui-icon.mui-icon-closeempty')[0].style.display = 'block';
		//		} else {
		//			mui('.mui-icon.mui-icon-closeempty')[0].style.display = 'none';
		//		}

		if(obj.value == '') {
			mui('.mui-icon.mui-icon-closeempty')[0].style.display = 'none';
		}
	}

	function edit_pwd() {
		var ck_ed = cheche_edit();
		if(ck_ed) {
			document.getElementById('reg').removeAttribute('disabled');
		} else {
			document.getElementById('reg').setAttribute('disabled', 'disabled');
		}
	}

	function cheche_edit() {
		if(document.getElementById('nickname').value == '') {
			return false;
		}
		if(document.getElementById('account').value.trim() == '') {
			return false;
		}
		/*
		if(!(/^1(3|4|5|7|8)\d{9}$/.test(document.getElementById('account').value))) {
			return false;
		}*/
		if(document.getElementById('password').value.trim() == '') {
			return false;
		}

		return true;
	}
	//点击国家/地区
	document.getElementById("nation").addEventListener('tap', function() {

		mui.openWindow({
			url: 'pages/login/nation_list.html',
			id: 'nation_list.html'
		});

	});

	
}(mui, document));